package com.dhms.vostok1.ui.main.select_mbr

import android.content.Intent
import android.os.Bundle
import android.text.method.LinkMovementMethod
import android.view.Gravity
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.dhms.mine.BR
import com.dhms.mine.R
import com.dhms.mine.databinding.ActivitySelectMemberBinding
import com.dhms.vostok1.adapter.SelectMemberAdapter
import com.dhms.vostok1.base.BaseActivity
import com.dhms.vostok1.routes.MineRoute
import com.dhms.vostok1.utils.SelectMemberPop
import com.dhms.vostok1.utils.UserInfoManager

@Route(path = MineRoute.MEMBER_SELECT)
class SelectMemberActivity : BaseActivity<ActivitySelectMemberBinding, SelectMemberViewModel>() {
    @Autowired
    @JvmField
    var json: String = ""//传入的Json

    private val selectMemberViewModel: SelectMemberViewModel by viewModels()
    private var adapter: SelectMemberAdapter? = null
    private var searchAdapter: SelectMemberAdapter? = null

    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_select_member
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initViewModel(): SelectMemberViewModel {
        return selectMemberViewModel
    }

    override fun initData() {
        super.initData()
        ARouter.getInstance().inject(this)
        adapter = SelectMemberAdapter()
        searchAdapter = SelectMemberAdapter()
        binding.recyclerview.adapter = adapter
        binding.searchList.adapter = searchAdapter
        binding.tvNav.movementMethod = LinkMovementMethod.getInstance()
        viewModel.default(json)
        getUser()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.backEvent.observe(this) {
            finish()
        }
        viewModel.dataList.observe(this) {
            adapter?.setNewInstance(it)
        }
        adapter?.setGroupItemClickListener {
            viewModel.getMbrById(it)
        }
        adapter?.setMemberSelectListener {
            viewModel.changeCheckedList(it)
        }
        viewModel.searchDataList.observe(this) {
            searchAdapter?.iSearch = true
            searchAdapter?.setNewInstance(it)
        }
        viewModel.searchKeyObservable.observe(this) {
            searchAdapter?.searchKey = it
        }
        searchAdapter?.setMemberSelectListener {
            viewModel.changeCheckedList(it)
        }
        viewModel.popEvent.observe(this) {
            val pop = SelectMemberPop(this, viewModel.checkedList)
            pop.showAtLocation(binding.bottomBar, Gravity.BOTTOM, 0, 0)
            pop.setOnDismissListener {
                viewModel.numString.value =
                    String.format(getString(R.string.select_num), viewModel.checkedList.size)
            }
        }
        viewModel.confirmEvent.observe(this) {
            setResult(RESULT_OK, Intent().putExtra("json", it))
            this.finish()
        }
    }

    private fun getUser() {
        val organizationId = UserInfoManager.organizationId ?: 0
        viewModel.organizationId = organizationId
    }
}